package com.example.weatherdemo;



import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherDemo extends Activity {

	final static String url="http://free.worldweatheronline.com/feed/weather.ashx?q=Ha+noi,Viet+nam&format=json&num_of_days=3&key=a4cda0550d163946132702";
	final static String REQUEST="request";
	final static String WEATHER="weather";
	final static String DATE="date";
	final static String QUERY="query";
	final static String TEMPMAXC="tempMaxC";
	final static String TEMPMINC="tempMinC";
	final static String WEATHERICON="weatherIconUrl";
	final static String DATA="data";
	TextView tvcity;
	TextView tvdata;
	TextView tvCmax;
	TextView tvCmin;
	TextView tvlink;
	ImageView img;
	
	JSONObject cityitem;
	JSONObject c;
	ProgressDialog pDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weather_demo);
		tvcity=(TextView)findViewById(R.id.tvcity);
		tvdata=(TextView)findViewById(R.id.tvdate);
		tvCmax=(TextView)findViewById(R.id.tvCmax);
		tvCmin=(TextView)findViewById(R.id.tvCmin);
		tvlink=(TextView)findViewById(R.id.tvlink);
		img=(ImageView)findViewById(R.id.img);
		/*ArrayList<String>listweather=new ArrayList<String>();
		XMLParser parser = new XMLParser();
		HashMap<String, String> list=new HashMap<String, String>();
		String xml=parser.getXmlFromUrl(url);
		Document doc=parser.getDomElement(xml);
		
		NodeList n=doc.getElementsByTagName(REQUEST);
		Element erequest=(Element)n.item(0);
		tvcity.setText(parser.getValue(erequest, QUERY));
		
		NodeList nl=doc.getElementsByTagName(WEATHER);
		Element e=(Element)nl.item(0);
		tvdata.setText(parser.getValue(e, DATE));
		tvCmin.setText(parser.getValue(e, TEMPMINC));
		tvCmax.setText(parser.getValue(e, TEMPMAXC));
		
		NodeList nlicon=e.getElementsByTagName("weatherIconUrl");
		Element eicon=(Element)nlicon.item(0);
		
	
		tvlink.setText(parser.getValue(eicon, "CDATA"));*/
		//new LoadImformation().execute();
		
		JSONParser jParser=new JSONParser();
		JSONObject object=jParser.getJSONFromUrl(url);
		try {
			JSONObject objectdata=object.getJSONObject("data");
			JSONArray weather=objectdata.getJSONArray("weather");
			JSONArray city=objectdata.getJSONArray(REQUEST);
			 cityitem=city.getJSONObject(0);
			tvcity.setText(cityitem.getString("query"));
			JSONObject c=weather.getJSONObject(1);
			String data=c.getString("date");
			Log.d("kt", data);
			tvdata.setText(c.getString("date"));
			tvCmax.setText(c.getString("tempMaxC"));
			tvCmin.setText(c.getString("tempMinC"));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		//loadData(url);
		Log.d("kt", "da chay den day");
	}

	public void loadData(String u){
		try{
			URL url=new URL(u);
		SAXParserFactory spf=SAXParserFactory.newInstance();
		SAXParser sp=spf.newSAXParser();
		XMLReader reader=sp.getXMLReader();
		WeatherHandler handler=new WeatherHandler();
		reader.setContentHandler(handler);
		reader.parse(new InputSource(url.openStream()));
		
		tvcity.setText(handler.getQUERY());
		tvdata.setText(handler.getDATE());
		tvCmin.setText(handler.getTEMPMINC());
		tvCmax.setText(handler.getTEMPMAXC());
		
		}catch(Exception e){
			Toast.makeText(this, "khong co du lieu", Toast.LENGTH_LONG).show();
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_weather_demo, menu);
		return true;
	}
public class LoadImformation extends AsyncTask<String, String, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			pDialog = new ProgressDialog(WeatherDemo.this);
			pDialog.setMessage(Html
					.fromHtml("<b>Search</b><br/>Loading Places..."));
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			JSONParser jParser=new JSONParser();
			JSONObject object=jParser.getJSONFromUrl(url);
			try {
				JSONObject objectdata=object.getJSONObject("data");
				JSONArray weather=objectdata.getJSONArray("weather");
				JSONArray city=objectdata.getJSONArray(REQUEST);
				JSONObject cityitem=city.getJSONObject(0);
				
				 c=weather.getJSONObject(1);
				
				
				
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			return null;
		}
		@Override
		protected void onPostExecute(String result) {
		pDialog.dismiss();
		try {
			tvcity.setText(cityitem.getString("query"));
			tvdata.setText(c.getString("date"));
			tvCmax.setText(c.getString("tempMaxC"));
			tvCmin.setText(c.getString("tempMinC"));
		} catch (JSONException e) {
			// TODO: handle exception
		}
		super.onPostExecute(result);
		}
		}
}

